﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


using System.Data;

using System.Web.UI.HtmlControls;
using Frame.Lib.Core.Dept;
using Frame.Lib.Core.Right;
using Frame.Lib.Core.User;
using Frame.Lib.Enums;
using Frame.Lib.Utilities;
using Jzy.DAL;

namespace Jzy.Web.DocManager.Dir
{
    public partial class SaveDirRight : System.Web.UI.Page
    {
        private string strDirID;

        protected void Page_Load(object sender, EventArgs e)
        {
            strDirID = Request["DirID"];
            if (!IsPostBack)
            {
                BindRight();
                DispRight();
            }
            AddAttr();
        }
        private void AddAttr()
        {

            JSUtil.ShowDeptUserTreeDlg(hidBrowse, txtBrowse, false, null);

            JSUtil.ShowDeptUserTreeDlg(hidAdd, txtAdd, false, null);

            JSUtil.ShowDeptUserTreeDlg(hidEdit, txtEdit, false, null);

            JSUtil.ShowDeptUserTreeDlg(hidDelete, txtDelete, false, null);

            JSUtil.ShowDeptUserTreeDlg(hidManage, txtManage, false, null);

            //txtBrowse.Style.Add("display", "none");

            txtAdd.Style.Add("display", "none");
    
            txtEdit.Style.Add("display", "none");
            txtDelete.Style.Add("display", "none");
 
            txtManage.Style.Add("display", "none");
          
        }

        private void DispRight()
        {
            RecordRightBO right = new RecordRightBO("DocDirRight");
            int bindID = int.Parse(strDirID);

            UserBO user = new UserBO();
            DeptBO dept = new DeptBO();

            //浏览            
            List<int> lstUserIDs = right.GetOwnerIDs(bindID, RecordRightName.Browse, RightOwnerType.User);
            List<string> lstUserNames = user.GetName(lstUserIDs);

            List<int> lstDeptIDs = right.GetOwnerIDs(bindID, RecordRightName.Browse, RightOwnerType.Dept);
            List<string> lstDeptNames = dept.GetName(lstDeptIDs);

            hidBrowse.Value = DeptBO.MergeDeptUser(lstDeptIDs, lstUserIDs);
            txtBrowse.Text = DeptBO.MergeDeptUser(lstDeptNames, lstUserNames);

            //增加            
            lstDeptIDs = right.GetOwnerIDs(bindID, RecordRightName.Add, RightOwnerType.Dept);
            lstDeptNames = dept.GetName(lstDeptIDs);

            lstUserIDs = right.GetOwnerIDs(bindID, RecordRightName.Add, RightOwnerType.User);
            lstUserNames = user.GetName(lstUserIDs);

            hidAdd.Value = DeptBO.MergeDeptUser(lstDeptIDs, lstUserIDs);
            txtAdd.Text = DeptBO.MergeDeptUser(lstDeptNames, lstUserNames);

            //删除            
            lstUserIDs = right.GetOwnerIDs(bindID, RecordRightName.Delete, RightOwnerType.User);
            lstUserNames = user.GetName(lstUserIDs);

            lstDeptIDs = right.GetOwnerIDs(bindID, RecordRightName.Delete, RightOwnerType.Dept);
            lstDeptNames = dept.GetName(lstDeptIDs);

            hidDelete.Value = DeptBO.MergeDeptUser(lstDeptIDs, lstUserIDs);
            txtDelete.Text = DeptBO.MergeDeptUser(lstDeptNames, lstUserNames);

            //编辑          
            lstDeptIDs = right.GetOwnerIDs(bindID, RecordRightName.Edit, RightOwnerType.Dept);
            lstDeptNames = dept.GetName(lstDeptIDs);

            lstUserIDs = right.GetOwnerIDs(bindID, RecordRightName.Edit, RightOwnerType.User);
            lstUserNames = user.GetName(lstUserIDs);

            hidEdit.Value = DeptBO.MergeDeptUser(lstDeptIDs, lstUserIDs);
            txtEdit.Text = DeptBO.MergeDeptUser(lstDeptNames, lstUserNames);

            //管理
            lstDeptIDs = right.GetOwnerIDs(bindID, RecordRightName.Manage, RightOwnerType.Dept);
            lstDeptNames = dept.GetName(lstDeptIDs);

            lstUserIDs = right.GetOwnerIDs(bindID, RecordRightName.Manage, RightOwnerType.User);
            lstUserNames = user.GetName(lstUserIDs);

            hidManage.Value = DeptBO.MergeDeptUser(lstDeptIDs, lstUserIDs);
            txtManage.Text = DeptBO.MergeDeptUser(lstDeptNames, lstUserNames);
        }

        private void BindRight()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("name");
            dt.Columns.Add("txtRight");

            DataRow dr = dt.NewRow();
            dr["name"] = "浏览目录";
            dr["txtRight"] = txtBrowse.ClientID;
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["name"] = "增加文档";
            dr["txtRight"] = txtAdd.ClientID;
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["name"] = "修改目录";
            dr["txtRight"] = txtEdit.ClientID;
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["name"] = "删除目录";
            dr["txtRight"] = txtDelete.ClientID;
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["name"] = "管理目录";
            dr["txtRight"] = txtManage.ClientID;
            dt.Rows.Add(dr);

            rptRight.DataSource = dt.DefaultView;
            rptRight.DataBind();
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            int bindID = int.Parse(strDirID);
            RecordRightBO right = new RecordRightBO(OTableName.DocDirRight);
            List<int> lstDeptIDs = new List<int>();
            List<int> lstUserIDs = new List<int>();

            //浏览                
            DeptBO.SplitDeptUser(hidBrowse.Value, lstDeptIDs, lstUserIDs);
            right.SaveOwnerIDs(lstUserIDs, bindID, RecordRightName.Browse, RightOwnerType.User);
            right.SaveOwnerIDs(lstDeptIDs, bindID, RecordRightName.Browse, RightOwnerType.Dept);

            //增加文档       
            DeptBO.SplitDeptUser(hidAdd.Value, lstDeptIDs, lstUserIDs);
            right.SaveOwnerIDs(lstDeptIDs, bindID, RecordRightName.Add, RightOwnerType.Dept);
            right.SaveOwnerIDs(lstUserIDs, bindID, RecordRightName.Add, RightOwnerType.User);

            //删除
            DeptBO.SplitDeptUser(hidDelete.Value, lstDeptIDs, lstUserIDs);
            right.SaveOwnerIDs(lstUserIDs, bindID, RecordRightName.Delete, RightOwnerType.User);
            right.SaveOwnerIDs(lstDeptIDs, bindID, RecordRightName.Delete, RightOwnerType.Dept);

            //编辑                
            DeptBO.SplitDeptUser(hidEdit.Value, lstDeptIDs, lstUserIDs);
            right.SaveOwnerIDs(lstDeptIDs, bindID, RecordRightName.Edit, RightOwnerType.Dept);
            right.SaveOwnerIDs(lstUserIDs, bindID, RecordRightName.Edit, RightOwnerType.User);

            //管理（可增加文档)
            DeptBO.SplitDeptUser(hidManage.Value, lstDeptIDs, lstUserIDs);
            right.SaveOwnerIDs(lstDeptIDs, bindID, RecordRightName.Manage, RightOwnerType.Dept);
            right.SaveOwnerIDs(lstUserIDs, bindID, RecordRightName.Manage, RightOwnerType.User);

            string strScript = "top.delDivFrm();";
            JSUtil.ResponseJS(strScript);
        }
        protected void rptRight_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                HtmlGenericControl item = (HtmlGenericControl)e.Item.FindControl("item");
                if (e.Item.ItemIndex == 0)
                {
                    item.Style.Add("background-color", "#cccccc");
                }
                HiddenField hidRight = (HiddenField)e.Item.FindControl("hidRight");
                item.Attributes.Add("onclick", "selectRight('" + hidRight.Value + "',this)");
            }
        }
    }
}
